package parser

import (
	"GraduationProject/public/models"
	"GraduationProject/worker/fengchedm/fengchedm_conf"
	"regexp"
	"strconv"
)

var (
	categoryRegex = regexp.MustCompile(`<div class="tame">[^<]*<h2><a href="([^"]*)"[^>]*>([^<]*)`)
)

func Category(seed models.Record, content []byte) (seeds []models.Record, results []models.Record) {
	categorys := categoryRegex.FindAllStringSubmatch(string(content), -1)

	seeds = make([]models.Record, 0, len(categorys))
	results = []models.Record{{
		NotElastic:   true,
		SeedID:       seed.ID,
		Url:          seed.Url,
		Params:       seed.Params,
		FunctionName: seed.FunctionName,
		UniqueKey:    strconv.Itoa(int(seed.ID)) + "-" + seed.Url,
		Result:       string(content),
	}}
	for _, v := range categorys {
		seeds = append(seeds, models.Record{
			NotElastic:   true,
			SeedID:       seed.ID,
			Url:          fengchedm_conf.Prefix + v[1],
			FunctionName: fengchedm_conf.FengchedmAnime,
			Remarks:      v[2],
		})
	}

	return
}
